/**
 * AUTO_COPYRIGHT_SUB_TAG
 */
package com.rim.samples.device.push;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;


/**
 * Stores persistent data in the push sample application.
 * 
 * <p>
 * Includes the following data - current registration status, message received count, last message received timestamp, push
 * configuration, log messages and SIM card information.
 */
public class PersistentStorage implements Persistable {

    private static long ID = 0x26bd52087559fde8L; // PersistentStorage
    private static PersistentStorage ps;

    // Total count of received messages
    private int totalMessageCount;
    // Timestamp when the last message was received
    private long lastMessageReceived;

    // Config settings
    private PushConfig config;

    // Logger messages
    //private LogMessages log;

    // SIM card info to detect SIM swap event
    private byte[] simCardInfo;
    
    // Save the user name while registered
    private String username;
    
    private PushMessage[] messages;
    
    // Specifies whether the application has been already launched
    // at least once
    private boolean onceLaunched;
    
    // Specifies whether the application has been successfully registered
    // with Content Provider and BPS server
    private boolean registered;

    static {
        PersistentObject po = PersistentStore.getPersistentObject( ID );
        ps = (PersistentStorage) po.getContents();
        if( ps == null ) {
            ps = new PersistentStorage();
            po.setContents( ps );
            po.commit();
        }
    }
//
    private PersistentStorage() {
        //messages = new PushMessage[0];
    }

    /**
     * Increments message count and commits to the persistent storage. Also assigns current time to the last received timestamp.
     */
    public static void incTotalMessageCount() {
        ps.totalMessageCount++;
        ps.lastMessageReceived = System.currentTimeMillis();
        PersistentObject.commit( ps );
    }

    /**
     * Returns received message count
     */
    public static int getTotalMessageCount() {
        return ps.totalMessageCount;
    }
    
    /**
     * Set message count
     */
    public static void setTotalMessageCount( int total ) {
        ps.totalMessageCount = total;
        PersistentObject.commit( ps );
    }

    /**
     * Returns timestamp when the last message was received.
     */
    public static long getLastMessageReceived() {
        return ps.lastMessageReceived;
    }

    public static PushConfig getConfig() {
        return ps.config;
    }

    public static void setConfig( PushConfig config ) {
        ps.config = config;
        PersistentObject.commit( ps );
    }

//    public static LogMessages getLog() {
//        return ps.log;
//    }
//
//    public static void setLog( LogMessages log ) {
//        ps.log = log;
//        PersistentObject.commit( ps );
//    }

    public static byte[] getSimCardInfo() {
        return ps.simCardInfo;
    }

    public static void setSimCardInfo( byte[] simCardInfo ) {
        ps.simCardInfo = simCardInfo;
        PersistentObject.commit( ps );
    }
    
    public static String getUsername() {
        return ps.username;
    }

    public static void setUsername( String username ) {
        ps.username = username;
        PersistentObject.commit( ps );
    }
    
    public static PushMessage[] getMessages() {
        return ps.messages;
    }

    public static void setMessages( PushMessage[] messages ) {
        ps.messages = messages;
        PersistentObject.commit( ps );
    }
    
    public static boolean isOnceLaunched() {
        return ps.onceLaunched;
    }

    public static void setOnceLaunched( boolean onceLaunched ) {
        ps.onceLaunched = onceLaunched;
        PersistentObject.commit( ps );
    }
    
    public static boolean isRegistered() {
        return ps.registered;
    }

    public static void setRegistered( boolean registered ) {
        ps.registered = registered;
        PersistentObject.commit( ps );
    }

    /**
     * Erases persistent store, used for debug purposes only
     */
    public static void erase() {
        PersistentStore.destroyPersistentObject( ID );
    }

}
